<template>
  <div class="three-wrapper">
    <div class="box" ref="box"></div>
  </div>
</template>

<script>
import * as THREE from 'three'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'
import {
  CSS3DRenderer,
  CSS3DObject
} from 'three/examples/jsm/renderers/CSS3DRenderer'

const clock = new THREE.Clock()
const FPS = 30 // 设置渲染频率为30FBS，也就是每秒调用渲染器render方法大约30次
const renderT = 1 / FPS // 单位秒  间隔多长时间渲染渲染一次
let timeS = 0

export default {
  components: {},
  props: {},
  data() {
    return {
      el: null,
      scene: null,
      cssScene: null, // CSS3D场景
      camera: null,
      renderer: null,
      cssRenderer: null, // CSS3D渲染器
      controls: null,
      animationFrameId: null
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {
    this.el = this.$refs['box']
    this.clock = new THREE.Clock()

    this.initThree()
    // 添加窗口resize监听
    window.addEventListener('resize', this.onWindowResize)
  },
  activated() {},
  deactivated() {},
  updated() {},
  beforeDestroy() {
    // 组件销毁时取消动画帧和移除事件监听
    if (this.animationFrameId) {
      cancelAnimationFrame(this.animationFrameId)
    }
    window.removeEventListener('resize', this.onWindowResize)
  },
  destroyed() {},
  methods: {
    initThree() {
      // 初始化 - 开始
      this.initScene() // 核心： 场景
      this.initCamera() // 核心: 相机
      this.initRender() // 核心 WebGL渲染器
      this.initCSS3DRenderer() // 初始化CSS3D渲染器
      this.initLight()
      this.initControls()
      this.animate()
      this.scene.add(this.setGridHelper(100 * 2, 10 * 2))
      this.scene.add(this.setAxesHelper(100000))
      // 初始化 - 结束

      // 添加物体
      this.addMesh()
      // 添加CSS3D文字卡片
      this.addCSS3DCards()
    },

    addMesh() {
      const geometry = new THREE.BoxGeometry(10, 10, 10)
      const material = new THREE.MeshLambertMaterial({
        color: new THREE.Color('#00ff00'),
        transparent: true
      })
      const mesh = new THREE.Mesh(geometry, material)
      this.scene.add(mesh)
    },

    // 添加CSS3D文字卡片
    addCSS3DCards() {
      // 创建简单文字卡片
      this.createSimpleTextCard()
    },

    // 创建简单文字卡片
    createSimpleTextCard() {
      const element = document.createElement('div')
      element.className = 'css3d-card simple-card'
      element.innerHTML = `
        <div class="card-content">
          <h3>简单文字卡片</h3>
          <p>这是一个使用CSS3DRenderer创建的卡片</p>
          <p>支持所有CSS3特性</p>
        </div>
      `

      const cssObject = new CSS3DObject(element)
      cssObject.position.set(0, 20, 0)
      cssObject.scale.set(0.1, 0.1, 0.1) // 缩放卡片大小
      this.cssScene.add(cssObject)
    },

    setGridHelper(size, divisions) {
      return new THREE.GridHelper(size, divisions)
    },

    setAxesHelper(size) {
      return new THREE.AxesHelper(size)
    },

    // 初始化场景
    initScene() {
      this.scene = new THREE.Scene()
      this.cssScene = new THREE.Scene() // 创建单独的CSS3D场景
    },

    // 初始化相机
    initCamera() {
      const asp = this.el.offsetWidth / this.el.offsetHeight
      this.camera = new THREE.PerspectiveCamera(45, asp, 0.1, 10000000)
      this.camera.position.set(300, 300, 300)
      this.camera.lookAt(this.scene.position)
      this.scene.add(this.camera)
    },

    initRender(clearColor = '#000') {
      const width = this.el.offsetWidth
      const height = this.el.offsetHeight
      this.renderer = new THREE.WebGLRenderer({
        antialias: true,
        alpha: true
      })
      this.renderer.setPixelRatio(window.devicePixelRatio)
      this.renderer.setSize(width, height)
      this.el.append(this.renderer.domElement)
      // this.renderer.setClearColor(clearColor, 1) // 不设置背景色
    },

    // 初始化CSS3D渲染器
    initCSS3DRenderer() {
      const width = this.el.offsetWidth
      const height = this.el.offsetHeight

      this.cssRenderer = new CSS3DRenderer()
      this.cssRenderer.setSize(width, height)
      this.cssRenderer.domElement.style.position = 'absolute'
      this.cssRenderer.domElement.style.top = '0'
      this.cssRenderer.domElement.style.left = '0'
      this.cssRenderer.domElement.style.pointerEvents = 'none' // 避免遮挡3D交互
      this.el.appendChild(this.cssRenderer.domElement)
    },

    initLight() {
      const ambientLight = new THREE.AmbientLight('#fff', 0.3)
      this.scene.add(ambientLight)
      const directionalLight = new THREE.DirectionalLight('#fff')
      directionalLight.position.set(30, 30, 30).normalize()
      this.scene.add(directionalLight)
    },

    initControls() {
      this.controls = new OrbitControls(
        this.camera,
        this.renderer.domElement,
        {}
      )
    },

    render() {
      const dt = this.clock.getDelta()
      const T = clock.getDelta()
      timeS = timeS + T
      if (timeS > renderT) {
        this.controls.update(dt)
        this.renderer.render(this.scene, this.camera)
        // 渲染CSS3D场景
        this.cssRenderer.render(this.cssScene, this.camera)
      }
    },

    animate() {
      this.render()
      this.animationFrameId = requestAnimationFrame(this.animate)
    },

    // 窗口大小变化时调整渲染器
    onWindowResize() {
      const width = this.el.offsetWidth
      const height = this.el.offsetHeight

      this.camera.aspect = width / height
      this.camera.updateProjectionMatrix()

      this.renderer.setSize(width, height)
      this.cssRenderer.setSize(width, height)
    }
  },
  filters: {}
}
</script>

<style scoped lang="scss">
.three-wrapper {
  height: 100%;
  position: relative;
  .box {
    height: 100%;
    width: 100%;
    background: url(./images/map-bg.png);
    background-size: cover;
    background-repeat: no-repeat;
    overflow: hidden;
    position: relative;
  }
}

/* CSS3D卡片样式 */
:deep(.css3d-card) {
  pointer-events: none;
  font-family: 'Microsoft YaHei', Arial, sans-serif;

  /* 简单卡片样式 */
  &.simple-card {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border-radius: 12px;
    padding: 20px;
    color: white;
    min-width: 200px;
    box-shadow: 0 8px 25px rgba(0, 0, 0, 0.3);
    border: 2px solid rgba(255, 255, 255, 0.1);

    .card-content {
      h3 {
        margin: 0 0 10px 0;
        font-size: 18px;
        font-weight: bold;
      }

      p {
        margin: 5px 0;
        font-size: 14px;
        opacity: 0.9;
      }
    }
  }
}
</style>
